﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using BySoft.Server.Common.Schema;
using BySoft.Server.Business;
using OneSoft.Web.UI.WebControls;
using BySoft.Server.Common.Enum;
using BySoft.Server.Consts;
using OneSoft.EntityCore;
using BySoft.Server.Common;
using System.Data.OleDb;
using System.IO;
using BySoft.Server.Common.Entity;
using BySoft.Web.Utility;


namespace BySoft.Web.Site
{
    public class AuthBasePage : BasePage
    {
        public List<string> SessionName
        {
            get
            {
                if (null == Session["GlobalSessionName"])
                {
                    return null;
                }
                else
                {
                    return (List<string>)Session["GlobalSessionName"];
                }
            }
            set
            {
                Session["GlobalSessionName"] = value;
            }
        }

        public List<TextBox> listtextbox = new List<TextBox>();
        public List<Label> listlabel = new List<Label>();
        public List<DropDownList> listddl = new List<DropDownList>();
        public List<HtmlInputText> listinputtext = new List<HtmlInputText>();
        public List<HiddenField> listhidden = new List<HiddenField>();
        public List<TextBox> listtb1 = new List<TextBox>();
        public List<HtmlInputText> listtb2 = new List<HtmlInputText>();
        public List<TextBox> listmoduletextbox = new List<TextBox>();

        #region 本地方法

        public void ControlInitial()
        {
            listddl.Clear();
            listhidden.Clear();
            listinputtext.Clear();
            listtextbox.Clear();
            listlabel.Clear();
            listtb1.Clear();
            listtb2.Clear();
            listmoduletextbox.Clear();
            foreach (Control ctl in this.Controls)
            {
                InitialControl(ctl.Controls);
            }
        }

        public void ControlInitial(GridViewRow gvr)
        {
            listddl.Clear();
            listhidden.Clear();
            listinputtext.Clear();
            listtextbox.Clear();
            listlabel.Clear();
            listtb1.Clear();
            listtb2.Clear();
            listmoduletextbox.Clear();

            foreach (Control ctl in gvr.Controls)
            {
                InitialControl(ctl.Controls);
            }
        }

        public DataTable BindDTByDT(DataTable dt, DataTable dt2)
        {
            DataTable dtCopy = dt.Copy();
            foreach (DataRow dr in dt2.Rows)
            {

                DataRow dr2 = dtCopy.NewRow();
                foreach (DataColumn dc in dt2.Columns)
                {
                    if (dr2.Table.Columns.Contains(dc.ColumnName))
                    {
                        dr2[dc.ColumnName] = dr[dc.ColumnName].ToString();
                    }
                }
                dtCopy.Rows.Add(dr2.ItemArray);
                dtCopy.AcceptChanges();
            }
            dt = dtCopy.Copy();
            return dt;
        }

        public void ControlInitial(HtmlTable tBase,bool bExtDDL)
        {
            listddl.Clear();
            listhidden.Clear();
            listinputtext.Clear();
            listtextbox.Clear();
            listlabel.Clear();
            listtb1.Clear();
            listtb2.Clear();
            listmoduletextbox.Clear();
            foreach (Control ctl in tBase.Controls)
            {
                InitialControl(ctl.Controls, bExtDDL);
            }
        }

        public void InitialControl(ControlCollection objControlCollection, bool bExtDDL)
        {
            foreach (System.Web.UI.Control objControl in objControlCollection)
            {
                if (objControl.HasControls())
                {
                    InitialControl(objControl.Controls, bExtDDL);
                }
                else
                {
                    if (objControl is System.Web.UI.WebControls.TextBox)
                    {
                        listtextbox.Add(((TextBox)objControl));
                    }
                    if (!bExtDDL)
                    {
                        if (objControl is System.Web.UI.WebControls.DropDownList)
                        {
                            listddl.Add(((DropDownList)objControl));
                        }
                    }
                    if (objControl is System.Web.UI.HtmlControls.HtmlInputText)
                    {
                        listinputtext.Add(((HtmlInputText)objControl));
                    }
                    if (objControl is System.Web.UI.WebControls.HiddenField)
                    {
                        listhidden.Add(((HiddenField)objControl));
                    }
                    if (objControl is System.Web.UI.WebControls.Label)
                    {
                        listlabel.Add(((Label)objControl));
                    }
                    if (objControl.ID != null)
                    {
                        if (objControl.ID.ToString() == "txtName")
                        {
                            listtb1.Add(((TextBox)objControl));
                        }
                        if (objControl.ID.ToString() == "hidValue")
                        {
                            listtb2.Add((HtmlInputText)objControl);
                        }
                    }
                    if (objControl.Parent != null)
                    {
                        if (objControl.Parent.ToString() == "CustomControls.ModalTextBox")
                        {
                            if (objControl is System.Web.UI.WebControls.TextBox)
                            {
                                listmoduletextbox.Add(((TextBox)objControl));
                            }
                        }
                    }
                }
            }
        }


        //获取页面上textbox，dropdownlist，htmlinputtext，hiddenfield控件
        public void InitialControl(ControlCollection objControlCollection)
        {
            foreach (System.Web.UI.Control objControl in objControlCollection)
            {
                if (objControl.HasControls())
                {
                    InitialControl(objControl.Controls);
                }
                else
                {
                    if (objControl is System.Web.UI.WebControls.TextBox)
                    {
                        listtextbox.Add(((TextBox)objControl));
                    }
                    if (objControl is System.Web.UI.WebControls.DropDownList)
                    {
                        listddl.Add(((DropDownList)objControl));
                    }
                    if (objControl is System.Web.UI.HtmlControls.HtmlInputText)
                    {
                        listinputtext.Add(((HtmlInputText)objControl));
                    }
                    if (objControl is System.Web.UI.WebControls.HiddenField)
                    {
                        listhidden.Add(((HiddenField)objControl));
                    }
                    if (objControl is System.Web.UI.WebControls.Label)
                    {
                        listlabel.Add(((Label)objControl));
                    }
                    if (objControl.ID != null)
                    {
                        if (objControl.ID.ToString() == "txtName")
                        {
                            listtb1.Add(((TextBox)objControl));
                        }
                        if (objControl.ID.ToString() == "hidValue")
                        {
                            listtb2.Add((HtmlInputText)objControl);
                        }
                    }
                    if (objControl.Parent != null)
                    {
                        if (objControl.Parent.ToString() == "CustomControls.ModalTextBox")
                        {
                            if (objControl is System.Web.UI.WebControls.TextBox)
                            {
                                listmoduletextbox.Add(((TextBox)objControl));
                            }
                        }
                    }
                }
            }
        }

        public void SetControlAttr(EntityBase ent, HtmlTable tBase)
        {
            ControlInitial(tBase);
            SetControlAttrLength(ent);
        }

        public void SetControlAttr(EntityBase ent, OneSoft.Web.UI.WebControls.SGridView gv)
        {
            foreach (GridViewRow gvr in gv.Rows)
            {
                ControlInitial(gvr);
                SetControlAttrLength(ent);
            }
        }

        private void SetControlAttrLength(EntityBase ent)
        {
            for (int i = 0; i < listtextbox.Count; i++)
            {
                int len = listtextbox[i].ID.ToString().Length;
                string strColName = listtextbox[i].ID.Substring(3, len - 3).ToUpper();

                foreach (ColumnInfo item in ent.OringTableSchema.AllColumnInfo)
                {
                    if (item.ColumnName.Equals(strColName))
                    {
                        if (item.DataType == typeof(String) && item.MaxLength != -1)
                        {
                            listtextbox[i].MaxLength = item.MaxLength;
                        }
                        break;
                    }
                }
            }
        }

        //控件数据绑定
        public void BindControl(DataTable dt)
        {
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int tbindex = 0; tbindex < listtb1.Count; tbindex++)
                    {
                        string column = listtb1[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_txtName") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 13) + "txt";
                            if (dt.Columns.Contains(columntext))
                            {
                                listtb1[tbindex].Text = dt.Rows[0][columntext].ToString();
                            }
                        }
                    }
                    for (int tbindex = 0; tbindex < listtb2.Count; tbindex++)
                    {
                        string column = listtb2[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_hidValue") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 11);
                            if (dt.Columns.Contains(columntext))
                            {
                                listtb2[tbindex].Value = dt.Rows[0][columntext].ToString();
                            }
                        }
                    }
                    for (int modindex = 0; modindex < listmoduletextbox.Count; modindex++)
                    {
                        int lengthtext = listmoduletextbox[modindex].ID.ToString().Length;
                        string columntext = listmoduletextbox[modindex].ID.Substring(3, lengthtext - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            listmoduletextbox[modindex].Text = dt.Rows[0][columntext].ToString();
                        }
                    }
                    for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
                    {
                        int lengthtext = listtextbox[textRowIndex].ID.ToString().Length;
                        string columntext = listtextbox[textRowIndex].ID.Substring(3, lengthtext - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            listtextbox[textRowIndex].Text = dt.Rows[0][columntext].ToString().Replace("1900-1-1 00:00:00", "");
                        }
                    }
                    for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
                    {
                        int lengthddl = listddl[ddlRowIndex].ID.ToString().Length;
                        string columntext = listddl[ddlRowIndex].ID.Substring(3, lengthddl - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            listddl[ddlRowIndex].SelectedValue = dt.Rows[0][columntext].ToString().Replace("1900-1-1 00:00:00", "");
                        }
                    }
                    for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
                    {
                        int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length;
                        string columninputtext = listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext - 3);
                        if (dt.Columns.Contains(columninputtext))
                        {
                            listinputtext[inputtextRowIndex].Value = dt.Rows[0][columninputtext].ToString().Replace("1900-1-1 00:00:00", "");
                        }
                    }
                    for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
                    {
                        int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length;
                        string columntext = listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            listhidden[hiddenRowIndex].Value = dt.Rows[0][columntext].ToString().Replace("1900-1-1 00:00:00", "");
                        }
                    }
                    for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
                    {
                        int lengthhidden = listlabel[labelRowIndex].ID.ToString().Length;
                        string columntext = listlabel[labelRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            listlabel[labelRowIndex].Text = dt.Rows[0][columntext].ToString().Replace("1900-1-1 00:00:00", "");
                        }
                    }
                }
            }
        }

        public void BindDataTableByGridViewUpdate(OneSoft.Web.UI.WebControls.SGridView gv, DataTable dt, int iUpdateIndex)
        {
            if (null == dt || dt.Rows.Count == 0)
            {
                return;
            }
            foreach (GridViewRow gvr in gv.Rows)
            {
                if (gvr.RowIndex != iUpdateIndex)
                {
                    continue;
                }
                gv.EditIndex = gvr.RowIndex;
                ControlInitial(gvr);
                if (dt.Rows[gvr.RowIndex] != null)
                {
                    for (int i = 0; i < listtextbox.Count; i++)
                    {
                        if (listtextbox[i].ID == null)
                            continue;
                        int len = listtextbox[i].ID.ToString().Length;
                        string strColName = listtextbox[i].ID.Substring(3, len - 3);
                        if (dt.Columns.Contains(strColName))
                        {
                            if (dt.Columns[strColName].DataType == typeof(Int32) || dt.Columns[strColName].DataType == typeof(Decimal))
                            {
                                if (listtextbox[i].Text == "")
                                {
                                    dt.Rows[gvr.RowIndex][strColName] = DBNull.Value;
                                }
                                else
                                {
                                    dt.Rows[gvr.RowIndex][strColName] = listtextbox[i].Text;
                                }
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][strColName] = listtextbox[i].Text;
                            }
                        }
                    }
                    for (int i = 0; i < listinputtext.Count; i++)
                    {
                        if (listinputtext[i].ID == null)
                            continue;
                        int len = listinputtext[i].ID.ToString().Length;
                        string strColName = listinputtext[i].ID.Substring(2, len - 2);
                        if (listinputtext[i].ID.ToLower().StartsWith("cp") && dt.Columns.Contains(strColName))//如果是以cp开始的话，则为日期控件
                        {
                            dt.Rows[gvr.RowIndex][strColName] = listinputtext[i].Value;
                        }
                    }
                }
            }
        }

        public void DBtoControlHead(DataTable dt, HtmlTable tBase, string strUrl)
        {
            ControlInitial(tBase);
            DBtoControlHeadData(dt, tBase, strUrl);
        }

        public void DBtoControlHead(object control, DataTable dt, HtmlTable tBase, string strUrl)
        {
            ControlInitial(tBase);
            DBtoControlHeadData(control, dt, strUrl);
        }

        public void DBtoControlHead(object control, DataTable dt, HtmlTable tBase, string strUrl,bool bExtDDL)
        {
            ControlInitial(tBase, bExtDDL);
            DBtoControlHeadData(control, dt, strUrl);
        }

        public void DBtoControlHead(object control, DataTable dt, string strUrl)
        {
            ControlInitial();
            DBtoControlHeadData(control, dt, strUrl);
        }

        private void DBtoControlHeadData(object control, DataTable dt, string strUrl)
        {
            if (null == dt || dt.Rows.Count < 1)
            {
                if (!string.IsNullOrEmpty(strUrl))
                {
                    UIHelper.AlertMessageGoToURL(this.Page, "载入数据失败", strUrl);
                }
                return;
            }
            DataRow row = dt.Rows[0];
            for (int i = 0; i < listtextbox.Count; i++)
            {
                int len = listtextbox[i].ID.ToString().Length;
                string strColName = listtextbox[i].ID.Substring(3, len - 3);
                listtextbox[i].Text = HaveCol(dt, strColName) ? CommonMethod.GetString(row[strColName]) : "";
            }
            for (int i = 0; i < listlabel.Count; i++)
            {
                int len = listlabel[i].ID.ToString().Length;
                string strColName = listlabel[i].ID.Substring(3, len - 3);
                listlabel[i].Text = HaveCol(dt, strColName) ? CommonMethod.GetString(row[strColName]) : "";
            }
            for (int i = 0; i < listddl.Count; i++)
            {
                int len = listddl[i].ID.ToString().Length;
                string strColName = listddl[i].ID.Substring(3, len - 3);
                string strTep = (HaveCol(dt, strColName) ? CommonMethod.GetString(row[strColName]) : "").Trim();
                listddl[i].SelectedValue = strTep;
            }
            for (int i = 0; i < listinputtext.Count; i++)
            {
                int len = listinputtext[i].ID.ToString().Length;
                string strColName = listinputtext[i].ID.Substring(2, len - 2);
                if (listinputtext[i].ID.ToLower().StartsWith("cp") && !string.IsNullOrEmpty(row[strColName].ToString()))//如果是以cp开始的话，则为日期控件
                {
                    listinputtext[i].Value = HaveCol(dt, strColName) ? CommonMethod.ConvertToDate(row[strColName]) : "";
                }
            }
        }

        private bool HaveCol(DataTable dt, string strColName)
        {
            foreach (DataColumn item in dt.Columns)
            {
                if (item.ColumnName.Equals(strColName))
                {
                    return true;
                }
            }
            return false;
        }

        private void DBtoControlHeadData(DataTable dt, HtmlTable tBase, string strUrl)
        {
            if (null == dt || dt.Rows.Count < 1)
            {
                UIHelper.AlertMessageGoToURL(this.Page, "载入数据失败", strUrl);
                return;
            }
            DataRow row = dt.Rows[0];
            for (int i = 0; i < listtextbox.Count; i++)
            {
                int len = listtextbox[i].ID.ToString().Length;
                string strColName = listtextbox[i].ID.Substring(3, len - 3);
                listtextbox[i].Text = CommonMethod.GetString(row[strColName]);
            }
            for (int i = 0; i < listlabel.Count; i++)
            {
                if (listlabel[i].ID.Contains("rfv") ||
                    listlabel[i].ID.Contains("rev"))
                {
                    continue;
                }
                int len = listlabel[i].ID.ToString().Length;
                string strColName = listlabel[i].ID.Substring(3, len - 3);
                listlabel[i].Text = CommonMethod.GetString(row[strColName]);
            }
            for (int i = 0; i < listddl.Count; i++)
            {
                int len = listddl[i].ID.ToString().Length;
                string strColName = listddl[i].ID.Substring(3, len - 3);
                listddl[i].SelectedValue = CommonMethod.GetString(row[strColName]);
            }
            for (int i = 0; i < listinputtext.Count; i++)
            {
                int len = listinputtext[i].ID.ToString().Length;
                string strColName = listinputtext[i].ID.Substring(2, len - 2);
                if (listinputtext[i].ID.ToLower().StartsWith("cp") && !string.IsNullOrEmpty(row[strColName].ToString()))//如果是以cp开始的话，则为日期控件
                {
                    listinputtext[i].Value = CommonMethod.ConvertToDate(row[strColName]);
                }
            }
        }

        public void BindDRByControl(DataRow dr)
        {
            ControlInitial();
            for (int i = 0; i < dr.ItemArray.Length; i++)
            {
                for (int tbindex = 0; tbindex < listtb2.Count; tbindex++)
                {
                    string column = listtb2[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                    int lengthtext = column.Length;
                    string columntext = "";
                    if (column.IndexOf("_hidValue") > 0)
                    {
                        columntext = column.Substring(2, lengthtext - 11);
                        if (dr.Table.Columns.Contains(columntext))
                        {
                            dr[columntext] = listtb2[tbindex].Value;
                        }
                    }
                }
                for (int tbindex = 0; tbindex < listtb1.Count; tbindex++)
                {
                    string column = listtb1[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                    int lengthtext = column.Length;
                    string columntext = "";
                    if (column.IndexOf("_txtName") > 0)
                    {
                        columntext = column.Substring(2, lengthtext - 13) + "txt";
                        if (dr.Table.Columns.Contains(columntext))
                        {
                            dr[columntext] = listtb1[tbindex].Text;
                        }
                    }
                }
                for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
                {
                    int lengthtext = listtextbox[textRowIndex].ID.ToString().Length - 3;
                    string disStr = listtextbox[textRowIndex].ID.Substring(3, lengthtext);
                    if (listtextbox[textRowIndex].Text.Trim() != "" && dr.Table.Columns.Contains(disStr))
                    {
                        dr[disStr] = listtextbox[textRowIndex].Text;
                    }
                    //else
                    //{
                    //    dr[disStr]=DBNull.Value;
                    //}
                }
                for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
                {
                    int lengthddl = listddl[ddlRowIndex].ID.ToString().Length - 3;
                    string disStr = listddl[ddlRowIndex].ID.Substring(3, lengthddl);
                    string disStrText = disStr.Substring(0, disStr.Length - 4) + "Text";
                    if (dr[disStr] != null)
                    {
                        dr[disStr] = listddl[ddlRowIndex].SelectedValue;
                    }
                    if (listddl[ddlRowIndex].SelectedValue != "" && dr.Table.Columns.Contains(disStrText))
                    {
                        dr[disStrText] = listddl[ddlRowIndex].SelectedItem.Text;
                    }
                    //else
                    //{
                    //    dr[disStrText]="";
                    //}
                }
                for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
                {
                    int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length - 3;
                    string disStr = listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext);
                    if (listinputtext[inputtextRowIndex].Value.Trim() != "" && dr.Table.Columns.Contains(disStr))
                    {
                        dr[disStr] = listinputtext[inputtextRowIndex].Value;
                    }
                    //else
                    //{
                    //    dr[disStr]=DBNull.Value;
                    //}
                }
                for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
                {
                    int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length - 3;
                    string disStr = listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden);
                    if (dr.Table.Columns.Contains(disStr))
                    {
                        dr[disStr] = listhidden[hiddenRowIndex].Value;
                    }
                }
                for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
                {
                    int lengthlabel = listlabel[labelRowIndex].ID.ToString().Length - 3;
                    string disStr = listlabel[labelRowIndex].ID.Substring(3, lengthlabel);
                    if (dr.Table.Columns.Contains(disStr))
                    {
                        dr[disStr] = listlabel[labelRowIndex].Text;
                    }
                }
            }
        }

        public void ControlInitial(HtmlTable tBase)
        {
            listddl.Clear();
            listhidden.Clear();
            listinputtext.Clear();
            listtextbox.Clear();
            listlabel.Clear();
            listtb1.Clear();
            listtb2.Clear();
            listmoduletextbox.Clear();
            foreach (Control ctl in tBase.Controls)
            {
                InitialControl(ctl.Controls);
            }
        }

        protected EntityBase ControlToEntity(bool bAdd, EntityBase ebOri)
        {
            return ExeControlToEntity(bAdd, ebOri, null);
        }

        private void SetEntityData(EntityBase eb, string strColName, string strValue, string[] strFields)
        {
            if (null == strFields)
            {
                eb.SetData(strColName, strValue);
            }
            else
            {
                if (HaveCol(strFields, strColName))
                {
                    eb.SetData(strColName, strValue);
                }
            }
        }

        private bool HaveCol(string[] strFields, string strColName)
        {
            bool b = false;
            strColName = strColName.ToLower();
            foreach (string ci in strFields)
            {
                if (ci.ToLower().Equals(strColName))
                {
                    b = true;
                    break;
                }
            }
            return b;
        }

        private EntityBase ExeControlToEntity(bool bAdd, EntityBase ebOri, string[] strFields)
        {
            EntityBase eb = ebOri;
            for (int i = 0; i < listtextbox.Count; i++)
            {
                int len = listtextbox[i].ID.ToString().Length;
                string strColName = listtextbox[i].ID.Substring(3, len - 3);
                SetEntityData(eb, strColName, listtextbox[i].Text, strFields);
            }
            for (int i = 0; i < listlabel.Count; i++)
            {
                if (!listlabel[i].ID.ToString().StartsWith("lbl"))
                    continue;
                int len = listlabel[i].ID.ToString().Length;
                string strColName = listlabel[i].ID.Substring(3, len - 3);
                SetEntityData(eb, strColName, listlabel[i].Text, strFields);
            }
            for (int i = 0; i < listddl.Count; i++)
            {
                int len = listddl[i].ID.ToString().Length;
                string strColName = listddl[i].ID.Substring(3, len - 3);
                SetEntityData(eb, strColName, listddl[i].SelectedValue, strFields);
            }
            for (int i = 0; i < listinputtext.Count; i++)
            {
                int len = listinputtext[i].ID.ToString().Length;
                string strColName = listinputtext[i].ID.Substring(2, len - 2);
                if (listinputtext[i].ID.ToLower().StartsWith("cp") && !string.IsNullOrEmpty(listinputtext[i].Value))//如果是以cp开始的话，则为日期控件
                {
                    SetEntityData(eb, strColName, listinputtext[i].Value, strFields);
                }
            }

            if (bAdd)
            {
                eb.SetData("Status", "1");
                eb.SetData("CreateUserId", CurrentUser.UserID);
                eb.SetData("CreateUserName", CurrentUser.UserName);
                eb.SetData("CreateDate", DateTime.Now);
                eb.SetData("ModifyUserId", CurrentUser.UserID);
                eb.SetData("ModifyUserName", CurrentUser.UserName);
                eb.SetData("ModifyDate", DateTime.Now);
            }
            else
            {
                eb.SetData("ModifyUserId", CurrentUser.UserID);
                eb.SetData("ModifyUserName", CurrentUser.UserName);
                eb.SetData("ModifyDate", DateTime.Now);
            }
            return eb;
        }

        public void DisableControl()
        {
            for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
            {
                listtextbox[textRowIndex].Enabled = false;
            }
            for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
            {
                listddl[ddlRowIndex].Enabled = false;
            }
            for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
            {
                listinputtext[inputtextRowIndex].Disabled = true;
            }
        }

        //绑定Entity
        public EntityBase BindEntity(EntityBase entity)
        {
            #region 利用反射及修改方案
            //Type t = entity.GetType();
            //PropertyInfo[] properties = t.GetProperties();
            //for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
            //{
            //    int lengthtext = listtextbox[textRowIndex].ID.ToString().Length - 3;
            //    for (int index = 0; index < properties.Length; index++)
            //    {
            //        String disStr = properties[index].ToString().Replace("System.String ", "").Replace("System.Nullable`1[System.Int32] ", "").Replace("System.Nullable`1[System.DateTime] ", "").Replace("System.Nullable`1[System.Decimal] ", "");
            //        if (disStr == listtextbox[textRowIndex].ID.Substring(3, lengthtext))
            //        {
            //            entity.SetData(listtextbox[textRowIndex].ID.Substring(3, lengthtext), listtextbox[textRowIndex].Text);
            //        }
            //    }
            //}

            //for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
            //{
            //    int lengthddl = listddl[ddlRowIndex].ID.ToString().Length - 3;
            //    for (int index = 0; index < properties.Length; index++)
            //    {
            //        String disStr = properties[index].ToString().Replace("System.String ", "").Replace("System.Nullable`1[System.Int32] ", "").Replace("System.Nullable`1[System.DateTime] ", "").Replace("System.Nullable`1[System.Decimal] ", "");
            //        if (disStr == listddl[ddlRowIndex].ID.Substring(3, lengthddl))
            //        {
            //            entity.SetData(listddl[ddlRowIndex].ID.Substring(3, lengthddl), listddl[ddlRowIndex].SelectedValue);
            //        }
            //    }
            //}
            //for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
            //{
            //    int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length - 3;
            //    for (int index = 0; index < properties.Length; index++)
            //    {
            //        String disStr = properties[index].ToString().Replace("System.String ", "").Replace("System.Nullable`1[System.Int32] ", "").Replace("System.Nullable`1[System.DateTime] ", "").Replace("System.Nullable`1[System.Decimal] ", "");
            //        if (disStr == listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext))
            //        {
            //            entity.SetData(listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext), listinputtext[inputtextRowIndex].Value);
            //        }
            //    }
            //}
            //for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
            //{
            //    int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length - 3;
            //    for (int index = 0; index < properties.Length; index++)
            //    {
            //        String disStr = properties[index].ToString().Replace("System.String ", "").Replace("System.Nullable`1[System.Int32] ", "").Replace("System.Nullable`1[System.DateTime] ", "").Replace("System.Nullable`1[System.Decimal] ", "");
            //        if (disStr == listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden))
            //        {
            //            entity.SetData(listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden), listhidden[hiddenRowIndex].Value);
            //        }
            //    }
            //}
            //for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
            //{
            //    int lengthlabel = listlabel[labelRowIndex].ID.ToString().Length - 3;
            //    for (int index = 0; index < properties.Length; index++)
            //    {
            //        String disStr = properties[index].ToString().Replace("System.String ", "").Replace("System.Nullable`1[System.Int32] ", "").Replace("System.Nullable`1[System.DateTime] ", "").Replace("System.Nullable`1[System.Decimal] ", "");
            //        if (disStr == listlabel[labelRowIndex].ID.Substring(3, lengthlabel))
            //        {
            //            entity.SetData(listlabel[labelRowIndex].ID.Substring(3, lengthlabel), listlabel[labelRowIndex].Text);
            //        }
            //    }
            //}

            ////修改方案一
            //for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
            //{
            //    string disStr1 = listtextbox[textRowIndex].ID.Substring(3);
            //    int i = 0;
            //    i = entity.OringTableSchema.AllColumnInfo.Where<ColumnInfo>(ci => ci.ColumnName == disStr1.ToUpper()).Count();
            //    if (i > 0)
            //    {
            //        entity.SetData(disStr1, listtextbox[textRowIndex].Text);
            //    }
            //}

            //修改方案二
            //for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
            //{
            //    string disStr1 = listtextbox[textRowIndex].ID.Substring(3);
            //    entity.SetData(disStr1, listtextbox[textRowIndex].Text);
            //}
            #endregion

            for (int tbindex = 0; tbindex < listtb1.Count; tbindex++)
            {
                int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                string column = "";
                if (listtb1[tbindex].ClientID.Contains("grdDetail"))
                {
                    column = listtb1[tbindex].ClientID.Substring(len, listtb1[tbindex].ClientID.Length - len);
                }
                else
                {
                    column = listtb1[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                }
                int lengthtext = column.Length;
                string columntext = "";
                if (column.IndexOf("_txtName") > 0)
                {
                    columntext = column.Substring(2, lengthtext - 13) + "txt";
                    if (listtb1[tbindex].Text != "")
                    {
                        entity.SetData(columntext, listtb1[tbindex].Text);
                    }
                    else
                    {
                        entity.SetData(columntext, null);
                    }
                }
            }
            for (int tbindex = 0; tbindex < listtb2.Count; tbindex++)
            {
                int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                string column = "";
                if (listtb2[tbindex].ClientID.Contains("grdDetail"))
                {
                    column = listtb2[tbindex].ClientID.Substring(len, listtb2[tbindex].ClientID.Length - len);
                }
                else
                {
                    column = listtb2[tbindex].ClientID.Replace("ctl00_EmptyMasteCPH_", "");
                }
                int lengthtext = column.Length;
                string columntext = "";
                if (column.IndexOf("_hidValue") > 0)
                {
                    columntext = column.Substring(2, lengthtext - 11);
                    if (listtb2[tbindex].Value != "")
                    {
                        entity.SetData(columntext, listtb2[tbindex].Value);
                    }
                    else
                    {
                        entity.SetData(columntext, null);
                    }
                }
            }
            for (int modindex = 0; modindex < listmoduletextbox.Count; modindex++)
            {
                int lengthtext = listmoduletextbox[modindex].ID.ToString().Length - 3;
                string disStr = listmoduletextbox[modindex].ID.Substring(3, lengthtext - 3);
                if (listmoduletextbox[modindex].Text.Trim() != "")
                {
                    entity.SetData(disStr, listmoduletextbox[modindex].Text);
                }
                else
                {
                    entity.SetData(disStr, null);
                }
            }

            for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
            {
                int lengthtext = listtextbox[textRowIndex].ID.ToString().Length - 3;
                string disStr = listtextbox[textRowIndex].ID.Substring(3, lengthtext);
                if (listtextbox[textRowIndex].Text.Trim() != "")
                {
                    entity.SetData(disStr, listtextbox[textRowIndex].Text);
                }
                else
                {
                    entity.SetData(disStr, null);
                }
            }
            for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
            {
                int lengthddl = listddl[ddlRowIndex].ID.ToString().Length - 3;
                string disStr = listddl[ddlRowIndex].ID.Substring(3, lengthddl);
                string disStrText = disStr.Substring(0, disStr.Length - 4) + "Text";
                entity.SetData(disStr, listddl[ddlRowIndex].SelectedValue);
                if (listddl[ddlRowIndex].SelectedValue != "")
                {
                    entity.SetData(disStrText, listddl[ddlRowIndex].SelectedItem.Text);
                }
                else
                {
                    entity.SetData(disStrText, "");
                }
            }
            for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
            {
                int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length - 3;
                string disStr = listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext);
                if (listinputtext[inputtextRowIndex].Value.Trim() != "")
                {
                    entity.SetData(disStr, listinputtext[inputtextRowIndex].Value);
                }
                else
                {
                    entity.SetData(disStr, null);
                }
            }
            for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
            {
                int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length - 3;
                string disStr = listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden);
                entity.SetData(disStr, listhidden[hiddenRowIndex].Value);
            }
            for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
            {
                int lengthlabel = listlabel[labelRowIndex].ID.ToString().Length - 3;
                string disStr = listlabel[labelRowIndex].ID.Substring(3, lengthlabel);
                entity.SetData(disStr, listlabel[labelRowIndex].Text);
            }
            return entity;
        }

        //绑定Entity
        public EntityBase BindEntity(EntityBase entity, DataRow dr, int index)
        {
            for (int i = 0; i < dr.ItemArray.Length; i++)
            {
                entity.SetData(dr.Table.Columns[i].ColumnName, dr.Table.Rows[index][i].ToString() == "" ? null : dr.Table.Rows[index][i].ToString());
            }
            return entity;
        }

        //textbox大小设定
        public void BindControlMax(DataTable dt)
        {
            if (dt.Rows.Count != 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
                    {
                        int lengthtext = listtextbox[textRowIndex].ID.ToString().Length;
                        string columntext = listtextbox[textRowIndex].ID.Substring(3, lengthtext - 3);
                        if (dt.Rows[i]["ColunmName"].ToString() == columntext)
                        {
                            if (dt.Rows[i]["typename"].ToString() == "varchar" || dt.Rows[i]["typename"].ToString() == "nvarchar")
                            {
                                listtextbox[textRowIndex].MaxLength = (Convert.ToInt32(dt.Rows[i]["size"].ToString()) / 2);
                            }
                            else
                            {
                                listtextbox[textRowIndex].MaxLength = (Convert.ToInt32(dt.Rows[i]["size"].ToString()) - Convert.ToInt32(dt.Rows[i]["scale"].ToString()) - 2);
                            }
                        }
                    }
                }
            }
        }

        public void BindControlMax(string tableName)
        {
            DataTable dtMax = BySoft.Server.Business.ClsComm.SelectGetField(tableName);
            ControlInitial();
            BindControlMax(dtMax);
        }

        public void BindGridViewMax(OneSoft.Web.UI.WebControls.SGridView gv, string tableName)
        {
            DataTable dtMax = BySoft.Server.Business.ClsComm.SelectGetField(tableName);
            foreach (GridViewRow gvr in gv.Rows)
            {
                ControlInitial(gvr);
                BindControlMax(dtMax);
            }
        }

        public void BindDataTableByGridView(OneSoft.Web.UI.WebControls.SGridView gv, DataTable dt)
        {
            if (dt.Rows.Count == 0)
            {
                return;
            }
            foreach (GridViewRow gvr in gv.Rows)
            {
                ControlInitial(gvr);
                if (dt.Rows[gvr.RowIndex] != null)
                {
                    for (int tbindex = 0; tbindex < listtb1.Count; tbindex++)
                    {
                        int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                        string column = listtb1[tbindex].ClientID.Substring(len, listtb1[tbindex].ClientID.Length - len);
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_txtName") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 13) + "txt";
                            if (dt.Columns.Contains(columntext))
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtb1[tbindex].Text;
                            }
                        }
                    }
                    for (int tbindex = 0; tbindex < listtb2.Count; tbindex++)
                    {
                        int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                        string column = listtb2[tbindex].ClientID.Substring(len, listtb2[tbindex].ClientID.Length - len);
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_hidValue") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 11);
                            if (dt.Columns.Contains(columntext))
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtb2[tbindex].Value;
                            }
                        }
                    }
                    for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
                    {
                        int lengthddl = listddl[ddlRowIndex].ID.ToString().Length;
                        string columntext = listddl[ddlRowIndex].ID.Substring(3, lengthddl - 3);
                        string disStrText = columntext.Substring(0, columntext.Length - 4) + "Text";
                        if (dt.Columns.Contains(columntext))
                        {
                            dt.Rows[gvr.RowIndex][columntext] = listddl[ddlRowIndex].SelectedValue;
                        }
                        if (dt.Columns.Contains(disStrText))
                        {
                            dt.Rows[gvr.RowIndex][disStrText] = listddl[ddlRowIndex].SelectedItem.Text;
                        }
                    }
                    for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
                    {
                        int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length;
                        string columninputtext = listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext - 3);
                        if (dt.Columns.Contains(columninputtext))
                        {
                            dt.Rows[gvr.RowIndex][columninputtext] = listinputtext[inputtextRowIndex].Value;
                        }
                    }
                    for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
                    {
                        int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length;
                        string columntext = listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            dt.Rows[gvr.RowIndex][columntext] = listhidden[hiddenRowIndex].Value;
                        }
                    }
                    for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
                    {
                        int lengthhidden = listlabel[labelRowIndex].ID.ToString().Length;
                        string columntext = listlabel[labelRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            if (listlabel[labelRowIndex].Text != "")
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listlabel[labelRowIndex].Text;
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][columntext] = DBNull.Value;
                            }
                        }
                    }
                    for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
                    {
                        int lengthtext = listtextbox[textRowIndex].ID.ToString().Length;
                        string columntext = listtextbox[textRowIndex].ID.Substring(3, lengthtext - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            if (dt.Columns[columntext].DataType == typeof(Int32) || dt.Columns[columntext].DataType == typeof(Decimal))
                            {
                                if (listtextbox[textRowIndex].Text == "")
                                {
                                    dt.Rows[gvr.RowIndex][columntext] = DBNull.Value;
                                }
                                else
                                {
                                    dt.Rows[gvr.RowIndex][columntext] = listtextbox[textRowIndex].Text;
                                }
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtextbox[textRowIndex].Text;
                            }
                        }
                    }
                }
            }
        }

        public void BindDataTableByGridView2(OneSoft.Web.UI.WebControls.SGridView gv, DataTable dt)
        {
            if (dt.Rows.Count == 0)
            {
                return;
            }
            foreach (GridViewRow gvr in gv.Rows)
            {
                gv.EditIndex = gvr.RowIndex;
                ControlInitial(gvr);
                if (dt.Rows[gvr.RowIndex] != null)
                {
                    for (int tbindex = 0; tbindex < listtb1.Count; tbindex++)
                    {
                        int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                        string column = listtb1[tbindex].ClientID.Substring(len, listtb1[tbindex].ClientID.Length - len);
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_txtName") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 13) + "txt";
                            if (dt.Columns.Contains(columntext))
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtb1[tbindex].Text;
                            }
                        }
                    }
                    for (int tbindex = 0; tbindex < listtb2.Count; tbindex++)
                    {
                        int len = "ctl00_EmptyMasteCPH_grdDetail_ctl02_".Length;
                        string column = listtb2[tbindex].ClientID.Substring(len, listtb2[tbindex].ClientID.Length - len);
                        int lengthtext = column.Length;
                        string columntext = "";
                        if (column.IndexOf("_hidValue") > 0)
                        {
                            columntext = column.Substring(2, lengthtext - 11);
                            if (dt.Columns.Contains(columntext))
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtb2[tbindex].Value;
                            }
                        }
                    }

                    for (int ddlRowIndex = 0; ddlRowIndex < listddl.Count; ddlRowIndex++)
                    {
                        int lengthddl = listddl[ddlRowIndex].ID.ToString().Length;
                        string columncode = listddl[ddlRowIndex].ID.Substring(3, lengthddl - 3);
                        string columntext = listddl[ddlRowIndex].ID.Substring(3, lengthddl - 7) + "Text";
                        if (dt.Columns.Contains(columncode))
                        {
                            dt.Rows[gvr.RowIndex][columncode] = listddl[ddlRowIndex].SelectedValue;
                            if (listddl[ddlRowIndex].SelectedValue != "")
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listddl[ddlRowIndex].SelectedItem.Text;
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][columntext] = "";
                            }
                        }
                    }
                    for (int inputtextRowIndex = 0; inputtextRowIndex < listinputtext.Count; inputtextRowIndex++)
                    {
                        int lengthinputtext = listinputtext[inputtextRowIndex].ID.ToString().Length;
                        string columninputtext = listinputtext[inputtextRowIndex].ID.Substring(3, lengthinputtext - 3);
                        if (dt.Columns.Contains(columninputtext))
                        {
                            dt.Rows[gvr.RowIndex][columninputtext] = listinputtext[inputtextRowIndex].Value;
                        }
                    }
                    for (int hiddenRowIndex = 0; hiddenRowIndex < listhidden.Count; hiddenRowIndex++)
                    {
                        int lengthhidden = listhidden[hiddenRowIndex].ID.ToString().Length;
                        string columntext = listhidden[hiddenRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            dt.Rows[gvr.RowIndex][columntext] = listhidden[hiddenRowIndex].Value;
                        }
                    }
                    for (int labelRowIndex = 0; labelRowIndex < listlabel.Count; labelRowIndex++)
                    {
                        int lengthhidden = listlabel[labelRowIndex].ID.ToString().Length;
                        string columntext = listlabel[labelRowIndex].ID.Substring(3, lengthhidden - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            if (listlabel[labelRowIndex].Text != "")
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listlabel[labelRowIndex].Text;
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][columntext] = DBNull.Value;
                            }
                        }
                    }
                    for (int textRowIndex = 0; textRowIndex < listtextbox.Count; textRowIndex++)
                    {
                        int lengthtext = listtextbox[textRowIndex].ID.ToString().Length;
                        string columntext = listtextbox[textRowIndex].ID.Substring(3, lengthtext - 3);
                        if (dt.Columns.Contains(columntext))
                        {
                            if (dt.Columns[columntext].DataType == typeof(Int32) || dt.Columns[columntext].DataType == typeof(Decimal))
                            {
                                if (listtextbox[textRowIndex].Text == "")
                                {
                                    dt.Rows[gvr.RowIndex][columntext] = DBNull.Value;
                                }
                                else
                                {
                                    dt.Rows[gvr.RowIndex][columntext] = listtextbox[textRowIndex].Text;
                                }
                            }
                            else
                            {
                                dt.Rows[gvr.RowIndex][columntext] = listtextbox[textRowIndex].Text;
                            }
                        }
                    }
                }
            }
        }
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //判断是否登录
            if (!CheckLogin())
            {
                //Session["PreviousPageUrl"] = this.Page.Request.Url;
                //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>TransferLogin('" + ConfigurationManager.AppSettings["LoginUrl"].ToString() + "')</script>");
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>TransferLogin('/Login.aspx')</script>");
                return;
            }

            if (!isCheck)
            {
                CheckRight(false);
            }
        }



        private bool isCheck
        {
            get
            {
                if (ViewState["isCheck"] == null)
                {
                    return false;
                }
                else
                {
                    return (bool)ViewState["isCheck"];
                }
            }
            set
            {
                ViewState["isCheck"] = value;
            }
        }

        /// <summary>
        /// 检查登录和权限
        /// </summary>
        /// <param name="isCheckDataRight">是否检查数据权限</param>
        private void CheckRight(bool isCheckDataRight)
        {
            Session["PreviousPageUrl"] = null;
            //判断是否登录
            if (!CheckLogin())
            {
                //Session["PreviousPageUrl"] = this.Page.Request.Url;
                //ClientScript.RegisterStartupScript(this.GetType(), "", "<script>TransferLogin('" + ConfigurationManager.AppSettings["LoginUrl"].ToString() + "')</script>");
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>TransferLogin('/Login.aspx')</script>");
                return;
            }

            #region 权限控制
            if (CurrentUser.UserID != CommonConsts.ADMIN_USER)//超级管理员不检查权限
            {
                if (ConfigurationManager.AppSettings["Right_Enable"].ToString().Equals("1"))//权限是否开启
                {
                    DataTable dtUserRole = SysUserrole.GetRoleByUser(CurrentUser.UserID);
                    if (dtUserRole.Select(SysRoleTable.C_ROLEID + "='" + CommonConsts.Admin_Role_ID + "'").Length == 0)//系统管理员不检查权限
                    {
                        if (this.Page.Request.Url.LocalPath.ToLower() != "/default.aspx")
                        {
                            //获取权限
                            string sUrl = "";
                            sUrl = this.Page.Request.Url.LocalPath.Substring(1);
                            if (this.Page.Request.Url.Query == "")
                            {
                                //sUrl = this.Page.Request.Url.LocalPath.Substring(1);
                            }
                            else
                            {
                                //针对连接中有参数的情况
                                bool isQuery = false;
                                //for (int i = 0; i < BaseEnum.WorkFromShortNameList().Count; i++)
                                //{
                                //    //工单录入
                                //    if (this.Page.Request.Url.Query.Split('&')[0].Contains("type=" + BaseEnum.WorkFromShortNameList().Values[i].ToString()))
                                //    {
                                //        isQuery = true;
                                //        break;
                                //    }
                                //}
                                //if (this.Page.Request.Url.Query.StartsWith("?ReportId"))
                                //{
                                //    //报表
                                //    isQuery = true;
                                //}
                                if (this.Page.Request.Url.Query.StartsWith("?ParamAdd"))
                                {
                                    //参数配置
                                    isQuery = true;
                                }
                                if (isQuery)
                                {
                                    //连接相同参数不同，决定不同的功能
                                    sUrl = this.Page.Request.Url.LocalPath.Substring(1) + this.Page.Request.Url.Query.Split('&')[0];
                                }
                                else
                                {
                                    sUrl = this.Page.Request.Url.LocalPath.Substring(1);
                                }
                                if (this.Page.Request.Url.Query.StartsWith("?modulemainid"))
                                {
                                    sUrl = this.Page.Request.Url.LocalPath.Substring(1) + this.Page.Request.Url.Query.Split('&')[0];
                                }
                            }
                            //sUrl = sUrl.Replace("Add.aspx", "List.aspx").Replace("Edit.aspx", "List.aspx").Replace("View.aspx", "List.aspx");
                            sUrl = sUrl.Replace("Pre.aspx", "Add.aspx").Replace("Apply.aspx", "List.aspx").Replace("Pur.aspx", "List.aspx").Replace("Rev.aspx", "List.aspx").Replace("Send.aspx", "List.aspx");
                            DataTable dtUserRight = SysUserrole.GetRoleRightByUserAndUrl(CurrentUser.UserID, sUrl);
                            //判断是否有该页面权限
                            if (dtUserRight.Rows.Count == 0)
                            {
                                Response.Redirect("~/SysManage/NoRights.aspx");
                            }
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains(dtUserRight.Rows[0][SysModuleTable.C_PAGETYPE].ToString()))
                            {
                                Response.Redirect("~/SysManage/NoRights.aspx");
                            }
                            //导出权限
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("E"))
                            {
                                if (this.Master.FindControl("EmptyMasteCPH") != null)
                                {
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("btnExport") != null)
                                    {
                                        (this.Master.FindControl("EmptyMasteCPH").FindControl("btnExport")).Visible = false;
                                    }
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("btnExport1") != null)
                                    {
                                        (this.Master.FindControl("EmptyMasteCPH").FindControl("btnExport1")).Visible = false;
                                    }
                                }
                            }
                            //新增权限
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("C"))
                            {
                                if (this.Master.FindControl("EmptyMasteCPH") != null)
                                {
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("btnAdd") != null)
                                    {
                                        (this.Master.FindControl("EmptyMasteCPH").FindControl("btnAdd")).Visible = false;
                                    }
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("upImport") != null)
                                    {
                                        (this.Master.FindControl("EmptyMasteCPH").FindControl("upImport")).Visible = false;
                                    }
                                    //对于区域设定的控管


                                }
                            }
                            //修改权限
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("U"))
                            {
                                if (this.Master.FindControl("EmptyMasteCPH") != null)
                                {
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult") != null)
                                    {
                                        if (!String.IsNullOrEmpty(dtUserRight.Rows[0][SysModuleTable.C_MODIFYCOLUMNLASTOFINDEX].ToString()) && dtUserRight.Rows[0][SysModuleTable.C_MODIFYCOLUMNLASTOFINDEX].ToString() != "0")
                                        {
                                            int nTemp = ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns.Count - int.Parse(dtUserRight.Rows[0][SysModuleTable.C_MODIFYCOLUMNLASTOFINDEX].ToString());
                                            ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns[nTemp].Visible = false;
                                        }
                                    }
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("btnUpdate") != null)
                                    {
                                        this.Master.FindControl("EmptyMasteCPH").FindControl("btnUpdate").Visible = false;
                                    }
                                }
                            }
                            //删除权限
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("D"))
                            {
                                if (this.Master.FindControl("EmptyMasteCPH") != null)
                                {
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult") != null)
                                    {
                                        if (!String.IsNullOrEmpty(dtUserRight.Rows[0][SysModuleTable.C_DELETECOLUMNLASTOFINDEX].ToString()) && dtUserRight.Rows[0][SysModuleTable.C_DELETECOLUMNLASTOFINDEX].ToString() != "0")
                                        {
                                            int nTemp = ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns.Count - int.Parse(dtUserRight.Rows[0][SysModuleTable.C_DELETECOLUMNLASTOFINDEX].ToString());
                                            ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns[nTemp].Visible = false;
                                        }
                                    }
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("btnDel") != null)
                                    {
                                        this.Master.FindControl("EmptyMasteCPH").FindControl("btnDel").Visible = false;
                                    }
                                }
                            }

                            //审核权限
                            if (!dtUserRight.Rows[0][SysRolerightsTable.C_RIGHTSCODE].ToString().Contains("A") && dtUserRight.Rows[0]["MODULERIGHTS"].ToString().Contains("A"))
                            {
                                if (this.Master.FindControl("EmptyMasteCPH") != null)
                                {
                                    if (this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult") != null)
                                    {
                                        //if (!String.IsNullOrEmpty(dtUserRight.Rows[0][SysModuleTable.C_DELETECOLUMNLASTOFINDEX].ToString()) && dtUserRight.Rows[0][SysModuleTable.C_DELETECOLUMNLASTOFINDEX].ToString() != "0")
                                        //{
                                            int nTemp = ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns.Count - 4;
                                            ((SGridView)this.Master.FindControl("EmptyMasteCPH").FindControl("grdResult")).Columns[nTemp].Visible = false;
                                        //}
                                    }
                                    //if (this.Master.FindControl("EmptyMasteCPH").FindControl("lbtnCheck") != null)
                                    //{
                                    //    this.Master.FindControl("EmptyMasteCPH").FindControl("lbtnCheck").Visible = false;
                                    //}
                                }
                            }
                        }
                        else
                        {
                            isCheck = true;
                        }
                    }
                    else
                    {
                        isCheck = true;
                    }
                }
                else
                {
                    isCheck = true;
                }
            }
            else
            {
                isCheck = true;
            }
            #endregion
        }

        protected void CheckDataRight()
        {
            CheckRight(true);
        }

    }
}
